import {
  ChangeDetectionStrategy,
  Component,
  EventEmitter,
  forwardRef,
  Input,
  OnInit,
  Output,
  ViewEncapsulation,
} from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { CheckbaseComponent, InputBoolean } from '../core/api';

@Component({
  selector: 'lv-checkbox-all, [lv-checkbox-all], [lvCheckboxAll]',
  exportAs: 'lvCheckboxAll',
  templateUrl: '../core/checkbase/checkbase.component.html',
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush,
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef((): typeof CheckboxAllComponent => CheckboxAllComponent),
      multi: true,
    },
  ],
  host: {
    '[class.lv-checkbase]': 'true',
    '[class.lv-checkbase-checked]': 'checked === true',
    '[class.lv-checkbase-button]': 'lvViewType === "button"',
    '[class.lv-checkbox-indeterminate]': '!!lvIndeterminate',
    '[attr.disabled]': 'disabled ? "" : null',
    '(click)': '_click($event)',
  },
})
export class CheckboxAllComponent extends CheckbaseComponent implements OnInit {
  @Input() @InputBoolean() lvIgnoreDisabled = true; // 设置全选状态时是否排除已禁用项
  @Output() lvCheckAllStatusChange = new EventEmitter<0 | 1 | 2>(); // 0 未选中 1 全选 2 部分选中

  /** @deprecated: 该属性废弃 */
  @Input() lvViewType: 'default' | 'button' | 'link' | 'custom' = 'default';

  ngOnInit(): void {
    this.type = 'checkbox';
  }

  markForCheck(): void {
    this.cdr.markForCheck();
  }
}
